package com.baosight.sczx.zm.ce.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiBlockMeta;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ef.ui.tree.TreeConstants;
import com.baosight.iplat4j.ef.ui.tree.TreeService;
import com.baosight.iplat4j.ep.QueryMap;

/**
 * <p>Title: ServiceZMCEB0.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张志芳
 * @version 1.0
 * <pre>Histroy:
 *          2011-12-15 上午11:38:23  张志芳  Create
 *</pre>
 */
public class ServiceZMCEB0 extends TreeService {
	private String fdKeyId = "root0";//设置根节点默认主键的值
	
	/**
	 * 查询树节点
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCE04</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-4-12下午03:53:24 张志芳
	 * </p>
	 *
	 */
	
	@SuppressWarnings("rawtypes")
	public EiInfo query(EiInfo inInfo) {
		QueryMap queryInfo = QueryMap.getQueryMap(inInfo);
		String p = (String) queryInfo.get(TreeConstants.TREE_NODE);
		String classLeaf = (String) queryInfo.get("classLeaf");
		List children = new ArrayList();
		if (p == null) {
			p = "$";
		}
		if ("$".equals(p)) {
			children = this.getTopNodes();
		} else {
			children = this.getChildNodes(p, classLeaf);
		}

		EiInfo outInfo = new EiInfo();
		EiBlock block = outInfo.addBlock(p);
		block.setBlockMeta(initMetaData());
		block.setRows(children);
		return outInfo;
	}
	
	/**
	 * 子节点构造
	 * <p>Function: getChildNodes</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCE04</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-4-12下午02:43:21 张志芳
	 * </p>
	 *
	 */	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public List getChildNodes(String arg0, String classLeaf) {
		if(classLeaf.equals("1"))
		{
			Map _argMap = new HashMap();
			_argMap.put(TreeConstants.TREE_NODE, arg0);
			List<Map> childNodes = dao.query("ZMCEStandredEquipment.queryChildrenNode", _argMap);
			for(Map map:childNodes)
			{
				String text=map.get("text").toString();
				map.put("showText", text);
			}
			return childNodes;
		}
		Map _argMap = new HashMap();
		_argMap.put(TreeConstants.TREE_NODE, arg0);
		List<Map> childNodes = dao.query("ZMCEEquipmentClass.queryChildrenNode", _argMap);
		for(Map map:childNodes)
		{
			String text=map.get("text").toString();
			map.put("showText", "(" + map.get("code") + ")" + text);
		}
		
		return childNodes;
	}

	/**
	 * 根节点构造
	 * <p>Function: getTopNodes</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCE04</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-4-12下午02:43:21 张志芳
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public List getTopNodes() {
		Map _argMap = new HashMap();
		_argMap.put("topNode", fdKeyId);
		List<Map> childNodes = dao.query("ZMCEEquipmentClass.queryChildrenNode", _argMap);
		for(Map map:childNodes)
		{
			map.put("showText", map.get("text"));
		}
		return childNodes;
	}

	/**
	 * <p>Function: initMetaData</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCE04</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-4-12下午02:43:21 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiBlockMeta initMetaData() {
		EiBlockMeta eiMetadata = new EiBlockMeta();

		EiColumn eiColumn = new EiColumn("label");
		eiColumn.setDescName("FD_KEY_ID");
		eiColumn.setNullable(false);
		eiColumn.setPrimaryKey(false);
		eiMetadata.addMeta(eiColumn);

		eiColumn = new EiColumn("showText");
		eiColumn.setDescName("名称");
		eiColumn.setNullable(false);
		eiColumn.setPrimaryKey(false);
		eiMetadata.addMeta(eiColumn);
		
		eiColumn = new EiColumn("text");
		eiColumn.setDescName("名称");
		eiColumn.setNullable(false);
		eiColumn.setPrimaryKey(false);
		eiMetadata.addMeta(eiColumn);

		eiColumn = new EiColumn("code");
		eiColumn.setDescName("编码");
		eiColumn.setNullable(false);
		eiColumn.setPrimaryKey(false);
		eiMetadata.addMeta(eiColumn);
		
		eiColumn = new EiColumn("toencode");
		eiColumn.setDescName("内码");
		eiColumn.setNullable(false);
		eiColumn.setPrimaryKey(false);
		eiMetadata.addMeta(eiColumn);
		
		eiColumn = new EiColumn("classLeaf");
		eiColumn.setDescName("设备分类的子节点");
		eiColumn.setNullable(false);
		eiColumn.setPrimaryKey(false);
		eiMetadata.addMeta(eiColumn);
		
		eiColumn = new EiColumn("leaf");
		eiColumn.setDescName("是否叶子");
		eiColumn.setType(EiConstant.COLUMN_TYPE_NUMBER);
		eiColumn.setNullable(false);
		eiColumn.setPrimaryKey(false);
		eiMetadata.addMeta(eiColumn);
		return eiMetadata;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List getChildNodes(String arg0) {
		return null;
	}

}
